package manager;

import java.rmi.AccessException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;

import rmi.ServeurRmiInterface;

public class Manager {
	private boolean connected = false;
	private ArrayList<String> messages = new ArrayList<String>();
	private ServeurRmiInterface serveurRmi;
	private String id_medecin="" ;
	
	public ArrayList <String> extraireMessages() {
		ArrayList <String> res = messages;
		messages = new ArrayList <String>();
		return res;
	}
	
	public ArrayList<String> getMessages() {
		return messages;
	}

	public void setMessages(ArrayList<String> messages) {
		this.messages = messages;
	}

	public boolean connect(String id, String pwd){
		ArrayList<String> messages = new ArrayList<String>();
		try {
			Registry registry = LocateRegistry.getRegistry(53000);
			serveurRmi = (ServeurRmiInterface) registry.lookup("serveurRMI");
			if(serveurRmi.authentifier(id, pwd)) {
				connected = true;
				id_medecin=id ;
			}else{
				connected = false;
			}
		} catch (AccessException e) {
			connected = false;
			e.printStackTrace();
			messages.add(e.getMessage());
		} catch (RemoteException e) {
			connected = false;
			e.printStackTrace();
			messages.add(e.getMessage());
		} catch (NotBoundException e) {
			connected = false;
			e.printStackTrace();
			messages.add(e.getMessage());
		}
		this.setMessages(messages);
		return connected;
	}
	
	public boolean isConnected(){
		return connected;
	}
	
	public void disConnected(){
		connected=false;
	}
	
	public ServeurRmiInterface getServeurRmi(){
		return serveurRmi;
	}
	
	public String getIdMedecin() {
		return id_medecin ;
	}
}
